package com.tarry.shop.minio.controller;

import com.tarry.shop.common.ResultEntity;
import com.tarry.shop.minio.MinioVO;
import com.tarry.shop.minio.util.MinioUtils;
import com.tarry.shop.validation.Add;
import com.tarry.shop.validation.Delete;
import com.tarry.shop.validation.Query;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.AbstractDocument;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

@RestController("minio")
@Api(value = "Minio接口")
@Validated
public class MinioController {
    @Resource
    MinioUtils minioUtils;

    @GetMapping("bucketExists")
    @ApiOperation(value = "判断桶是否存在")
    public ResultEntity<Boolean> bucketExists(@ApiParam(value = "桶名称" , required = true)
                                                  @NotEmpty(message = "桶名称不能为空") String bucketName){
        return ResultEntity.success(minioUtils.bucketExists(bucketName));
    }

    @DeleteMapping("deleteFile")
    @ApiOperation("删除文件对象")
    public ResultEntity<Boolean> removeObject(@ApiParam(value = "桶名称" , required = true)
                                                  @NotEmpty(message = "桶名称不能为空") String bucketName,
                                              @ApiParam(value = "文件对象名称" , required = true)
                                              @NotEmpty(message = "文件对象名称不能为空") String objectName){
        return ResultEntity.success(minioUtils.removeObject(bucketName, objectName));
    }

    @PostMapping("upload")
    @ApiOperation("上传文件到MINIO")
    public ResultEntity<MinioVO> uploadFile(@Valid @NotNull(message = "文件不能为空")
            @ApiParam(value = "上传文件", required = true) MultipartFile multipartFile,
                                            @ApiParam(value = "桶名称" , required = true)
                                            @NotEmpty(message = "桶名称不能为空") String bucketName,
                                            @ApiParam(value = "文件对象名称" , required = true)
                                                @NotEmpty(message = "文件对象名称不能为空") String objectName) {
        minioUtils.putObject(bucketName,multipartFile,objectName);
        String url = minioUtils.getObjectUrl(bucketName, objectName);
        MinioVO minioVO = MinioVO.builder().bucketName(bucketName).objectName(objectName).url(url).build();
        return ResultEntity.success(minioVO);
    }

    @GetMapping("getUrl")
    public ResultEntity<String> getUrl(@ApiParam(value = "桶名称" , required = true)
                                       @NotEmpty(message = "桶名称不能为空") String bucketName,
                                   @ApiParam(value = "文件对象名称" , required = true)
                                       @NotEmpty(message = "文件对象名称不能为空") String objectName){
        return ResultEntity.success(minioUtils.getObjectUrl(bucketName, objectName));

    }

    @PostMapping("download")
    @ApiOperation("下载文件")
    public void download(HttpServletResponse response,
                         @Validated(Add.class) MinioVO minioVO) {
        minioUtils.downloadFile(minioVO.getBucketName(),minioVO.getObjectName(),minioVO.getFileName(),response);
    }
}
